<template>
  <div id="contain">
    <div>
      <el-form
        :model="searchform"
        label-width="80px"
        class="seachform"
        style="width: 940px"
    >
        <el-form-item label="账号" style="width: 220px">
          <el-input v-model="searchform.loginName"></el-input>
        </el-form-item>
        <el-form-item label="角色" style="width: 200px">
          <el-select v-model="searchform.roleId" placeholder="选择角色">
            <el-option label="全部" value=''></el-option>
            <el-option label="管理员" value="1"></el-option>
            <el-option label="商家" value="2"></el-option>
            <el-option label="旅客" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名" style="width: 200px">
          <el-input v-model="searchform.username"></el-input>
        </el-form-item>
        <el-form-item label="真实姓名" style="width: 200px">
          <el-input v-model="searchform.realName"></el-input>
        </el-form-item>
        <el-button type="primary" @click="search" class="searchbtn" style="text-align: center"
        >搜索</el-button
        >
      </el-form>
      <el-table
        :data="tableList"
        style="width: 945px"
        stripe
        :row-style="rowStyle"
        :cell-style="cellStyle"
        height="790"
      >
        <el-table-column prop="loginName" label="账号" width="180" align="center">
        </el-table-column>
        <el-table-column
          prop="roleId"
          label="角色"
          align="center"
          style="color: red"
        ></el-table-column>
        <el-table-column prop="username" label="用户名" width="180" align="center">
        </el-table-column>
        <el-table-column prop="realName" label="真实姓名" align="center">
        </el-table-column>
        <el-table-column prop="mobile" label="手机号" align="center" width="120px">
        </el-table-column>
        <el-table-column prop="status" label="状态" align="center">
        </el-table-column>
        <el-table-column label="操作" align="center" width="220px">
          <template slot-scope="scope">
            <el-button style="color: red" type="mini" @click="forBid(scope.row)"
              >{{scope.row.status === '启用' ? '禁用' : '启用'}}</el-button
            >
            <el-button type="mini" @click="chongzhi(scope.row)"
              >重置密码</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="page"
        :page-sizes="[10, 15, 20]"
        :page-size="limit"
        layout="sizes, prev, pager, next"
        :total="totalCount"
      >
      </el-pagination>
    </div>
    <el-dialog title="修改密码" :visible.sync="dialog">
      <el-form :model="form2">
        <el-form-item label="新密码" :label-width="formLabelWidth">
          <el-input
            v-model="form2.newPwd1"
            style="width: 500px"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" :label-width="formLabelWidth">
          <el-input
            v-model="form2.newPwd2"
            style="width: 500px"
            type="password"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog = false">取 消</el-button>
        <el-button type="primary" @click="xiugai">点击修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      token: window.localStorage.getItem("token"),
      tableList: [],
      page: 1,
      limit: 10,
      totalCount: 0,
      form2: {},
      dialog: false,
      formLabelWidth: "120px",
      id: 1,
      searchform: {}
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    getList() {
      this.$axios({
        method: "post",
        url: "/sys/user/list",
        headers: { token: this.token },
        data: {
          page: this.page,
          limit: this.limit,
        },
      }).then((res) => {
        if (res.data.code === 0) {
          this.totalCount = res.data.page.totalCount;
          this.tableList = res.data.page.regard;
          this.tableList.forEach((item) => {
            item.roleId =
              item.roleId === 1
                ? "管理员"
                : item.roleId === 2
                ? "商家"
                : "旅客";
            item.status = item.status === 1 ? "启用" : "停用";
            item.realName = !item.realName ? "未填写" : item.realName;
            item.age = !item.age ? "未填写" : item.age;
            item.sex = !item.sex ? "未填写" : item.sex;
          });
        }
        if (res.data.code === 500) {
          this.$message({
            type: "error",
            message: res.data.msg,
          });
        }
      });
    },
    search () {
      this.searchform.page = this.page;
      this.searchform.limit = this.limit;
      let data = this.searchform;
      this.$axios({
        method: "post",
        url: "/sys/user/list",
        headers: { token: this.token },
        data: data,
      }).then((res) => {
        if (res.data.code === 0) {
          if (res.data.page) {
            this.tableList = res.data.page.regard;
            this.tableList.forEach((item) => {
              item.roleId =
                  item.roleId === 1
                      ? "管理员"
                      : item.roleId === 2
                      ? "商家"
                      : "旅客";
              item.status = item.status === 1 ? "启用" : "停用";
              item.realName = !item.realName ? "未填写" : item.realName;
              item.age = !item.age ? "未填写" : item.age;
              item.sex = !item.sex ? "未填写" : item.sex;
            });
          }
          else{
            this.tableList=[]
          }
        }
        if (res.data.code === 500) {
          this.$message({
            type: "error",
            message: res.data.msg,
          });
        }
      });
    },
    rowStyle() {
      return { height: "60px" };
    },
    forBid(k) {
      if (k.status === "启用") {
        this.$axios({
          method: "post",
          url: "/sys/user/update",
          headers: { token: this.token },
          data: {
            id: k.id,
            status: 0,
          },
        }).then((res) => {
          if (res.data.code === 0) {
            this.getList();
          }
          if (res.data.code === 500) {
            this.$message({
              type: "error",
              message: res.data.msg,
            });
          }
        });
      }
      if (k.status === "停用") {
        this.$axios({
          method: "post",
          url: "/sys/user/update",
          headers: { token: this.token },
          data: {
            id: k.id,
            status: 1,
          },
        }).then((res) => {
          if (res.data.code === 0) {
            this.getList();
          }
          if (res.data.code === 500) {
            this.$message({
              type: "error",
              message: res.data.msg,
            });
          }
        });
      }
    },
    cellStyle(k) {
      if (k.columnIndex === 5) {
        if (k.row.status === "启用") {
          return { color: "#33CC00" };
        } else {
          return { color: "red" };
        }
      }
      if (k.columnIndex === 1) {
        if (k.row.roleId === "旅客") {
          return { color: "#3399FF" };
        } else if (k.row.roleId === "商家") {
          return { color: "#FFCC00" };
        } else {
          return { color: "#FF6600" };
        }
      }
    },
    handleSizeChange(val) {
      this.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getList();
    },
    chongzhi(k) {
      // console.log(k);
      this.id = k.id;
      this.dialog = true;
    },
    xiugai() {
      if (!this.form2.newPwd1 || !this.form2.newPwd2) {
        this.$message.error("输入框不能为空");
        this.dialog = false;
        return;
      }
      if (this.form2.newPwd1 !== this.form2.newPwd2) {
        this.$message.error("两次密码不一致");
        this.dialog = false;
        return;
      }
      this.$axios({
        method: "post",
        url: "/sys/user/sys/reset/pwd",
        headers: { token: this.token },
        data: {
          newPwd: this.form2.newPwd1,
          userId: this.id,
        },
      }).then((res) => {
        if (!res.data.code === 0) {
          this.$message({
            type: "error",
            message: res.data.msg,
          });
        } else {
          this.$message({
            type: "success",
            message: "修改成功！",
          });
        }
      });
      this.dialog = false;
    },
  },
};
</script>
<style  scoped>
.seachform {
  margin: 10px 0 0 0;
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
}
.searchbtn {
  width: 80px;
  height: 40px;
  margin: 0 15px
}
</style>
